<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/task.jsp"%>
<%@ include file="/common/user.jsp"%>
<%
    final Integer doctorUid = token.getUid();
    String data = input.getDesc("data", "");
    String title = input.getDesc("title", "");
    String uids = input.getDesc("uids", "");
    final Long delay = input.getLong("delay", 0L);
    final Integer push = input.getInt("push", 0);    // 0表示不需推送  1表示推送病人 2表示推送医生 3表示推送全部
    String[] _uids = uids.split("\\|");
    if ("".equals(uids) || isEmpty(_uids)) {
        msg = new Message(30050, "请选择随访人员");
        Tools.encryptPrint(out, msg);
        return;
    }
    if ("".equals(title)) {
        msg = new Message(30050, "请添加标题");
        Tools.encryptPrint(out, msg);
        return;
    }
    if ("".equals(data)) {
        msg = new Message(30050, "创建失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    String _data = "";
    try {
        _data = Tools.decryptStr(data);
    } catch(Exception e) {
        _data = "";
    }
    if ("".equals(_data)) {
        msg = new Message(30050, "创建失败");
        Tools.encryptPrint(out, msg);
        return;
    }
    final List<Map> quests = JSONUtil.fromJson(_data, new TypeReference<List<Map>>(){});
    if (isEmpty(quests)) {
        msg = new Message(30050, "请至少添加一个随访内容");
        Tools.encryptPrint(out, msg);
        return;
    }
    MyTask task = new MyTask(_uids.length);
    Long time = new Date().getTime();
    for (int i = 0; i < _uids.length; i++) {
        Survey survey = new Survey();
        survey.setHospital(hospital);
        survey.setCategory(2);
        survey.setBelong("schedule");
        survey.setTitle(title);
        survey.setEnable(STATUS_ENABLE);
        task.runTask(task.new MyRunnable(task.new DBOperate() {

				@Override
				public Object operate(Object... objects) {
                    List<Integer> userIds = new ArrayList<Integer>();
                    userIds.add(Tools.getInt(objects[0]+"", 0));
                    Map<Integer, User> users = usersByUids(userIds);
                    User user = users.get(Tools.getInt(objects[0]+"", 0));

					Survey survey = (Survey)objects[1];
                    Integer _hospital = survey.getHospital();
                    Integer surveyId = (Integer)DBTools.db("basedb").addItem(survey);
                    if (isEmpty(surveyId)) {
                        Map _map = new HashMap();
                        if (user != null) {
                            _map.put("message", user.getName() + "创建随访计划失败");
                        }
                        return _map;
                    }
                    List<Map> _quests = (List<Map>)objects[2];
                    List<SurveyQuestion> sqs = new ArrayList<SurveyQuestion>();
                    Date date = new Date();
                    for (int i = 0; i < _quests.size(); i++) {
                        Map quest = _quests.get(i);
                        SurveyQuestion sq = new SurveyQuestion();
                        sq.setHospital(_hospital);
                        sq.setSurveyId(surveyId);
                        Integer questShowOrder = Tools.getInt(quest.get("show_order") + "", i + 1);
                        Long time = Tools.getLong(quest.get("time") + "", new Date().getTime());
                        String schedule = Tools.getDesc(quest.get("schedule") + "", "");
                        String unit = Tools.getDesc(quest.get("unit") + "", "");
                        String questTitle = Tools.getDesc(quest.get("title"), "");
                        String questDetail = Tools.getDesc(quest.get("detail"), "");
                        sq.setcTime(time);
                        sq.setTitle(questTitle);
                        sq.setDetail(questDetail);
                        sq.setShowOrder(questShowOrder);
                        sq.setType(0);
                        sq.setJson("schedule", schedule);
                        sq.setJson("unit", unit);
                        sqs.add(sq);
                        Integer scheduleId = (Integer)DBTools.db("basedb").addItem(sq);
                        Visit visit = new Visit();
                        visit.setHospital(_hospital);
                        visit.setDoctorUid(doctorUid);
                        visit.setPatientUid(Tools.getInt(objects[0]+"", 0));
                        visit.setSurveyId(scheduleId);
                        visit.setCategory(20);
                        visit.setCancel(push);
                        visit.setcTime(time);
                        visit.setSendTime(DateUtil.formatDateByFormat(date, "yyyy-MM-dd hh:mm:ss"));
                        visit.setCheckTime(DateUtil.formatDateByFormat(new Date(time), "yyyy-MM-dd HH:mm:ss"));
                        visit.setReconfirmedTime(DateUtil.formatDateByFormat(new Date(time - delay), "yyyy-MM-dd HH:mm:ss"));
                        visit.setIsconfirmed(0);
                        if (!isEmpty(user))
                            visit.setName(user.getName());
                        DBTools.db("basedb").addItem(visit);
                    }
                    Visit visit = new Visit();
                    visit.setHospital(_hospital);
                    visit.setDoctorUid(doctorUid);
                    visit.setPatientUid(Tools.getInt(objects[0]+"", 0));
                    visit.setSurveyId(surveyId);
                    visit.setCategory(2);
                    visit.setCancel(push);
                    visit.setReconfirmedTime(delay + "");
                    visit.setSendTime(DateUtil.formatDateByFormat(date, "yyyy-MM-dd hh:mm:ss"));
                    visit.setIsconfirmed(0);
                    if (!isEmpty(user))
                        visit.setName(user.getName());
                    DBTools.db("basedb").addItem(visit);
                    Information info = new Information();
                    info.setUid(Tools.getInt(objects[0]+"", 0));
                    info.setOid(surveyId+"");
                    info.setTitle(survey.getTitle());
                    info.setSumary("随访计划");
                    info.setContent(Tools.getDesc(user.getName(), "")+",您有一份随访计划注意查收");
                    info.setCategory(2);
                    DBTools.db("basedb").addItem(info);
					return null;
				}
			}, i, new Object[]{_uids[i], survey, quests}));
    }
    task.stopTask();
    Object[] results = task.getResult();
    List resultList = new ArrayList();
    for (int i = 0; i < results.length; i++) {
        Object obj = results[i];
        if (!isEmpty(obj)) {
            resultList.add(obj);
        }
    }
    msg.put("data", resultList);
    Tools.encryptPrint(out, msg);
%>
